package com.gismore.jsonp.core;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
import lombok.extern.log4j.Log4j2;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

@Log4j2
public class Collector {

	public static List<SongVO> collect(String filePath, File targetFile){
		log.info("开始分析歌单...");
		List<SongVO> songList = null;
		filePath = filePath.replace("\\\\","/");
		log.debug("filePath:{}",filePath);
		File sourcePlaylistFile = new File(filePath);
		log.debug(sourcePlaylistFile.exists());

		LineProcessor<Map<String, SongVO>> targetPlayListProcessor = new TargetPlaylistProcessor();
		Map<String, SongVO> targetSongMap = null;
		try {
			if (!targetFile.exists()) {
				targetSongMap = Maps.newHashMap();
			}else{
				targetSongMap = Files.readLines(targetFile, Charsets.UTF_8,  targetPlayListProcessor);
			}
			LineProcessor<List<SongVO>> sourcePlayListProcessor = new SourcePlaylistProcessor(targetSongMap);

			songList = Files.readLines(sourcePlaylistFile, Charsets.UTF_8, sourcePlayListProcessor);
			log.info("歌单分析完成.");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return songList;
	}
}
